package com.ywl.designpattern.prototype.deepclone;

public class Car implements Cloneable{

    private String brand;
    private Engine engine;

    public Car(String brand, Engine engine) {
        this.brand = brand;
        this.engine = engine;
    }

    public String getBrand() {
        return brand;
    }

    public Engine getEngine() {
        return engine;
    }

    @Override
    protected Car clone() {
        try {
            Car cloneCar = (Car) super.clone();
            // 深克隆：克隆 Engine对象
            cloneCar.engine = this.engine.clone();
            return cloneCar;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", engine=" + engine +
                '}';
    }
}
